package com.imooc.authorization.context;

import com.imooc.authorization.dto.Accordion;
import com.imooc.authorization.entity.User;
import javax.servlet.http.Cookie;
import java.util.*;

/**
 * Created by Administrator on 2016/8/18.
 */

public class LoginUserCache {

    private static Map<String,User> cache = new HashMap<String,User>();

    private static  Map<String,List<Accordion>> userAccordionMap=new HashMap<String,List<Accordion>>();


    public static void put(User user){

        cache.put(user.getName(),user);
        UserContext.setCurrent(user);
        setCookie(user);
    }

    public static User get(String username){

        return cache.get(username);
    }

    public  static void setCookie(User user) {
        int expire = 1800;

        String source = user.getName() +"$"+user.getPassword();

        byte[] result = Base64.getEncoder().encode(source.getBytes());
        Cookie cookie = new Cookie("auth",new String(result));

        cookie.setMaxAge(expire);
        ResponseContext.getCurrent().addCookie(cookie);

    }

    public static void remove(String username){

        cache.remove(username);
        Cookie cookie=new Cookie("auth",null);
        ResponseContext.getCurrent().addCookie(cookie);
        UserContext.setCurrent(null);
    }

    public static void setAccordions(String username,List<Accordion> accordions){

        userAccordionMap.put(username,accordions);
    }

    public static   List<Accordion>  getAccordions(String username){
        return userAccordionMap.get(username);
    }



}
